package com.examinantsdk.base.http;


import com.blankj.utilcode.util.LogUtils;
import com.examinantsdk.BuildConfig;
import com.examinantsdk.ExtKt;
import com.examinantsdk.base.ApiService;
import com.examinantsdk.base.Constants;
import com.examinantsdk.base.HeaderInterceptor;
import com.examinantsdk.base.http.converter.GsonConverterFactory;
import com.uuedu.examant.base.DecryptInterceptor;

import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;


public class RetrofitHelper {
    private static RetrofitHelper instance = null;
    private static OkHttpClient okHttpClient;
    public ApiService apiService;

    public RetrofitHelper(OkHttpClient okHttpClient) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.Urls.NCRE_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .build();
        apiService = retrofit.create(ApiService.class);
    }

    public static RetrofitHelper getInstance() {
        if (instance == null) {
            instance = new RetrofitHelper(getOkhttpClient());
        }
        return instance;
    }

    //okhttp连接的一些设置
    public static OkHttpClient getOkhttpClient() {
        if (BuildConfig.DEBUG) {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        } else {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }

        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)//连接超时时间
                .readTimeout(30, TimeUnit.SECONDS)//读超时时间
                .writeTimeout(30, TimeUnit.SECONDS)//写超时时间
                .retryOnConnectionFailure(true)//失败重连
                .addInterceptor(loggingInterceptor)
//                .addInterceptor(new MyInterceptor())
                .addInterceptor(new DecryptInterceptor())
                .addInterceptor(new HeaderInterceptor());
        ExtKt.exemptCertificate(builder);
        okHttpClient = builder.build();
        return okHttpClient;
    }

    //日志
    static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(message -> LogUtils.d("Api=>", message));

    //自定义SS验证相关类
    private static class TrustAllCerts implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }

    private static class TrustAllHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

    private static SSLSocketFactory createSSLSocketFactory() {
        SSLSocketFactory ssfFactory = null;
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{new TrustAllCerts()}, new SecureRandom());
            ssfFactory = sc.getSocketFactory();
        } catch (Exception e) {
        }
        return ssfFactory;
    }
}
